<?php
require "../util/php/mysql.php" ;
require "../util/php/mysql.php" ;
mysql_init ( "localhost", "tysny", "UTF8","db","db" ) ;
$zoneID = 53642;

$sql = " select id,fdURL,fdContentID from tyctn.tbFile where fdTypeID = 3 AND fdContentID in (select fdContentID from tysny.tbZone where id = $zoneID   )";
$res = mysql_exec ( $sql ) ;
while ( $row = mysql_fetch_assoc ( $res ) ) {
	$photo = $row["fdURL"];
	$info = get_file_info($photo);
	$width = $info["width"];
	$height = $info["height"];
	$contentID = $row["fdContentID"];
	$file =  $row["id"];
	
	$sql= " INSERT INTO tyctn.tbInteger(fdContentID,fdFileID,fdAttributeID,fdSourceID,fdValue)VALUES({$contentID},{$file},10,0,{$width})";
	mysql_exec ( $sql ) ;
	$sql= " INSERT INTO tyctn.tbInteger(fdContentID,fdFileID,fdAttributeID,fdSourceID,fdValue)VALUES({$contentID},{$file},14,0,{$height})";
	mysql_exec ( $sql ) ;
	
	echo "width:{$width},height:{$height}"."\r\n";
}

$sql = " select id,fdURL,fdContentID from tyctn.tbFile where fdTypeID = 3 AND fdContentID in (select fdContentID from tysny.tbPoi where fdZoneID = $zoneID   )";
$res = mysql_exec ( $sql ) ;
while ( $row = mysql_fetch_assoc ( $res ) ) {
	$photo = $row["fdURL"];
	$info = get_file_info($photo);
	$width = $info["width"];
	$height = $info["height"];
	$contentID = $row["fdContentID"];
	$file =  $row["id"];
	
	$sql= " INSERT INTO tyctn.tbInteger(fdContentID,fdFileID,fdAttributeID,fdSourceID,fdValue)VALUES({$contentID},{$file},10,0,{$width})";
	mysql_exec ( $sql ) ;
	$sql= " INSERT INTO tyctn.tbInteger(fdContentID,fdFileID,fdAttributeID,fdSourceID,fdValue)VALUES({$contentID},{$file},14,0,{$height})";
	mysql_exec ( $sql ) ;
	
	echo "width:{$width},height:{$height}"."\r\n";
}

$sql = " select id,fdURL,fdContentID from tyctn.tbFile where fdTypeID = 3 AND fdContentID in (select fdContentID from tysny.tbSite where fdZoneID = $zoneID   )";
$res = mysql_exec ( $sql ) ;
while ( $row = mysql_fetch_assoc ( $res ) ) {
	$photo = $row["fdURL"];
	$info = get_file_info($photo);
	$width = $info["width"];
	$height = $info["height"];
	$contentID = $row["fdContentID"];
	$file =  $row["id"];
	
	$sql= " INSERT INTO tyctn.tbInteger(fdContentID,fdFileID,fdAttributeID,fdSourceID,fdValue)VALUES({$contentID},{$file},10,0,{$width})";
	mysql_exec ( $sql ) ;
	$sql= " INSERT INTO tyctn.tbInteger(fdContentID,fdFileID,fdAttributeID,fdSourceID,fdValue)VALUES({$contentID},{$file},14,0,{$height})";
	mysql_exec ( $sql ) ;
	
	echo "width:{$width},height:{$height}"."\r\n";
}

function get_file_info($source){
	$width = 0;
	$height = 0;
	$size = 0;
	$ch = curl_init($source);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,15);
	curl_setopt($ch,CURLOPT_TIMEOUT,15);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

	$result = curl_exec($ch);
	$info = curl_getinfo($ch);
	$imgSize = $info["size_download"];
	curl_close($ch);
	
	try{
		$size = getimagesize('data://image/jpeg;base64,'. base64_encode($result));
	}catch(Exception $e){
	}
	
	if (!empty($size)) {
		$width = !is_null($size[0]) ? $size[0] : 0;
		$height = !is_null($size[1]) ? $size[1] : 0;
	}

	return array("file_size" => $imgSize, "width" => $width, "height" => $height);
}


?>